//
//  SelfDefineTabBarViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/23.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "SelfDefineTabBarViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface SelfDefineTabBarViewController ()

@property (nonatomic, retain) UIButton* selectedBtn;

@end

@implementation SelfDefineTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    [self creatSelfTabBar];
}

- (void)creatSelfTabBar
{
    // 创建自定义tabbarView
    CGRect rect = CGRectMake(0, HEIGHT - 49, WIDTH, 49);
    UIView* myView = [[UIView alloc] init];
    myView.backgroundColor = [UIColor blackColor];
    myView.alpha = 0.7;
    myView.frame = rect;
    [self.view addSubview:myView];
    // 隐藏系统自带tabbar
    self.tabBar.hidden = YES;
    // 创建四个tabbar按键
    for (NSInteger i = 0; i < 4; i++) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 4001 + i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake((WIDTH / 4) * i, 0, WIDTH / 4, 49);
        btn.backgroundColor = [UIColor clearColor];
        // 根据i值 改变按键图片
        switch (i) {
            case 0:
                [btn setImage:[UIImage imageNamed:@"tabnav-suggest"] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"tabnav-suggest-after"] forState:UIControlStateSelected];
                break;
            case 1:
                [btn setImage:[UIImage imageNamed:@"tabnav-topic"] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"tabnav-topic-after"] forState:UIControlStateSelected];
                break;
            case 2:
                [btn setImage:[UIImage imageNamed:@"tabnav-cp"] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"tabnav-cp-after"] forState:UIControlStateSelected];
                break;
            case 3:
                [btn setImage:[UIImage imageNamed:@"tabnav-me"] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:@"tabnav-me-after"] forState:UIControlStateSelected];
                break;
            default:
                break;
        }
        
        [myView addSubview:btn];
        if (i == 0) {
            btn.selected = YES;
            self.selectedBtn = btn;
        }
    }
}

- (void)clickBtn:(UIButton*)button
{
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    //2.再将当前按钮设置为选中
    button.selected = YES;
    //    button.selected = !button.selected;
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;
    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)
    self.selectedIndex = button.tag % 4001;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
